var defalutAwardsId = 0;
var defalutAwardsSubId = 0;
var defalutAwardsSubStatus = 0;
var defalutAwardsDateType = 1;
var onloading = false;
var searchKey = '';
var layer;
layui.use(['layer'], function(){
    layer = layui.layer;
});

$(function() {
    // 避免重复请求
    if (onloading) return;
    onloading = true;
    defalutAwardsId = getQueryVariable("id");
    if (defalutAwardsId == undefined){
        layer.msg("无效的活动地址");
        onloading = false;
        return;
    }
    // 数据默认加载奖项介绍
    $.ajaxSettings.async = false;
    $.get("/portals/vote/jsp/voteAwards/info.jsp?id="+defalutAwardsId, function(resp){
        if (resp.code == 200) {
            var json = resp.data;
            defalutAwardsDateType = json.dateType;
            defalutAwardsSubStatus = json.status;
            $("#voteAwardsAbout").text(json.about);
            if (defalutAwardsDateType == 1){
                $("#voteAwardsAbout").text(json.about);
                $("#voteAwardsRegular").text(json.regular);
            } else {
                $(".voteAwardsRegular").hide();
            }
        }else {
            console.log(resp.msg);
        }
    });
    // 数据默认加载子奖项列表
    $.get("/portals/vote/jsp/front/awardsSubData.jsp?awardsId="+defalutAwardsId, function(resp){
        var html = '';
        if (resp.code == 200){
            resp.data.forEach(function(obj){
                if (defalutAwardsSubId==0) {
                    defalutAwardsSubId = obj.id;
                    defalutAwardsSubStatus = obj.status;
                    html +='<div class="tc title db" onclick="selectedVoteAwardsSubFunc(this)"  awards_sub_id="'+obj.id+'"  awards_sub_status="'+obj.status+'">'+this.subStringFunc(obj.name)+'</div>';
                } else {
                    html +='<div class="tc title" onclick="selectedVoteAwardsSubFunc(this)"  awards_sub_id="'+obj.id+'"  awards_sub_status="'+obj.status+'">'+this.subStringFunc(obj.name)+'</div>';
                }
            })
        }else {
            console.log(resp.msg)
        }
        $("#voteAwardsSubnameNames").empty();
        $("#voteAwardsSubnameNames").append(html);
    });
    // 数据默认加载名单列表
    $.get("/portals/vote/jsp/front/names.jsp?awardsSubnameId="+defalutAwardsSubId, function(resp){
        var html='';
        if (resp.code == 200){
            var index = 1;
            resp.data.forEach(function(json){
                if (json.winStatus){                                                  // 是否有获奖标识
                    html +='<div class="people getJiang"  >';
                    html +='<div class="hj"><img src="../img/static/hj.png" alt=""></div>' +
                        '   <div class="jz"><img src="../img/static/jz.png" alt=""></div>';
                } else {
                    html +='<div class="people"  >';
                }
                html +=' <div class="people-img"  onclick="lookDetailFunc('+json.id+')">';
                html +='    <img src="'+json.nominatorPhoto+'" class="img">';
                if (defalutAwardsDateType==1){
                    html +='    <div class="img-ps">';
                    html +='        票数'+json.votesNum;
                    html +='    </div>';
                }
                html +=' </div>';
                if (json.nominatorType==1) {
                    html +=' <div class="people-info">';
                    html +='    <span class="w">姓名：</span>';
                    html +='    <span>'+json.memberNames+'</span>';
                    html +=' </div>';
                    html +=' <div class="people-info">';
                    html +='    <span class="w">部门：</span>';
                    html +='    <span>'+json.nominatorDeptName+'</span>';
                    html +=' </div>';
                    html += ' <div class="people-info">';
                    html += '    <span class="w">职位：</span>';
                    html += '    <span>' + json.nominatorPosition + '</span>';
                    html += ' </div>';
                }else {
                    html +=' <div class="people-info">';
                    html +='    <span class="w">团队：</span>';
                    html +='    <span>'+json.memberNames+'</span>';
                    html +=' </div>';
                    html += ' <div class="people-info">';
                    html += '    <span class="w">部门：</span>';
                    html += '    <span>' + json.applicantDeptName + '</span>';
                    html += ' </div>';
                }
                html +=' <div class="people-flower" onclick="shwoMedel('+json.id+')">' ;
                html +='    <div >' ;
                html +='        <span class="inb"><img src="../img/static/flower.png" class="fl" alt="送花"> <span class="ml10 fl f12 c9e9e9e">'+json.flowerNum+'</span> </span>' ;
                html +='    </div>' ;
                html +='    <div class="l"> ' ;
                html +='        <span  class="inb"><img src="../img/static/message.png" class="fl" alt="留言"><span class="ml10 fl f12 c9e9e9e">'+json.messageNum+'</span></span>';
                html +='    </div>' ;
                html +=' </div>' ;
                if (defalutAwardsDateType==1 && defalutAwardsSubStatus==1){
                    html +=' <div class="people-btn" id="peopleBtn" onclick="lookDetailFunc('+json.id+')"> 投票 </div>';
                }else if (defalutAwardsDateType==1){
                    html +=' <div class="people-btn-gray" id="peopleBtn"> 投票 </div>';
                }
                html +=' <div class="people-btn"  onclick="lookDetailFunc('+json.id+')">';
                html +=' 详情';
                html +=' </div>';
                html +='</div>';
                index++;
            });
        } else {
            console.log(resp.msg)
        }
        $("#nameList").empty();
        $("#nameList").append(html);
    });
    onloading = false;
});

/****
 * 选中子奖项，加载名单
 * @param this_
 */
function selectedVoteAwardsSubFunc(this_){
    // 避免重复请求
    if (onloading) return;
    onloading = true;
    // CSS 切换
    $(".tc").removeClass('db');
    $(this_).addClass('db');
    // 筛选条件
    searchKey = $("#searchBtn").val("");
    defalutAwardsSubId = $(this_).attr("awards_sub_id");
    defalutAwardsSubStatus = $(this_).attr("awards_sub_status");
    // 情况展示数据
    $("#nameList").empty();
    $("#nameList").append('<div class="tc c9e9e9e">数据加载中</div>');
    // 数据默认加载名单列表
    $.get("/portals/vote/jsp/front/names.jsp?awardsSubnameId="+defalutAwardsSubId, function(resp){
        var html='';
        if (resp.code == 200){
            var index = 1;
            if (resp.data.length == 0){
                html +='<div class="tc c9e9e9e">暂无数据</div>';
            } else {
                resp.data.forEach(function(json){
                    if (json.winStatus){                                                  // 是否有获奖标识
                        html +='<div class="people getJiang"  >';
                        html +='<div class="hj"><img src="../img/static/hj.png" alt=""></div>' +
                            '   <div class="jz"><img src="../img/static/jz.png" alt=""></div>';
                    } else {
                        html +='<div class="people"  >';
                    }
                    html +=' <div class="people-img" onclick="lookDetailFunc('+json.id+')">';
                    html +='    <img src="'+json.nominatorPhoto+'" class="img">';
                    if (defalutAwardsDateType==1){
                        html +='    <div class="img-ps">';
                        html +='        票数'+json.votesNum;
                        html +='    </div>';
                    }
                    if (json.nominatorType==1) {
                        html +=' </div>';
                        html +=' <div class="people-info">';
                        html +='    <span class="w">姓名：</span>';
                        html +='    <span>'+json.memberNames+'</span>';
                        html +=' </div>';
                        html += ' <div class="people-info">';
                        html += '    <span class="w">部门：</span>';
                        html += '    <span>' + json.nominatorDeptName + '</span>';
                        html += ' </div>';
                        html += ' <div class="people-info">';
                        html += '    <span class="w">职位：</span>';
                        html += '    <span>' + json.nominatorPosition + '</span>';
                        html += ' </div>';
                    }else {
                        html +=' </div>';
                        html +=' <div class="people-info">';
                        html +='    <span class="w">团队：</span>';
                        html +='    <span>'+json.memberNames+'</span>';
                        html +=' </div>';
                        html += ' <div class="people-info">';
                        html += '    <span class="w">部门：</span>';
                        html += '    <span>' + json.applicantDeptName + '</span>';
                        html += ' </div>';
                    }

                    html +=' <div class="people-flower" onclick="shwoMedel('+json.id+')">' ;
                    html +='    <div >' ;
                    html +='        <span class="inb"><img src="../img/static/flower.png" class="fl" alt="送花"> <span class="ml10 fl f12 c9e9e9e">'+json.flowerNum+'</span> </span>' ;
                    html +='    </div>' ;
                    html +='    <div class="l"> ' ;
                    html +='        <span  class="inb"><img src="../img/static/message.png" class="fl" alt="留言"><span class="ml10 fl f12 c9e9e9e">'+json.messageNum+'</span></span>';
                    html +='    </div>' ;
                    html +=' </div>' ;
                    if (defalutAwardsDateType==1 && defalutAwardsSubStatus==1){
                        html +=' <div class="people-btn" id="peopleBtn" onclick="lookDetailFunc('+json.id+')"> 投票 </div>';
                    }else if (defalutAwardsDateType==1){
                        html +=' <div class="people-btn-gray" id="peopleBtn"> 投票 </div>';
                    }
                    html +=' <div class="people-btn"  onclick="lookDetailFunc('+json.id+')">';
                    html +=' 详情';
                    html +=' </div>';
                    html +='</div>';
                    index++;
                });
            }
        } else {
            console.log(resp.msg)
        }
        $("#nameList").empty();
        $("#nameList").append(html);
    });
    onloading = false;
}

/***
 * 条件搜索
 */
function searchFunc() {
    // 避免重复请求
    if (onloading) return;
    onloading = true;
    // 筛选条件
    searchKey = $("#searchInput").val();
    // 同步加载
    $.ajaxSettings.async = true;
    // 数据默认加载名单列表
    $.get("/portals/vote/jsp/front/names.jsp?awardsSubnameId="+defalutAwardsSubId+"&searchKey="+searchKey, function(resp){
        var html='';
        if (resp.code == 200){
            var index = 1;
            if (resp.data.length == 0){
                html +='<div class="tc c9e9e9e">暂无数据</div>';
            } else {
                resp.data.forEach(function(json){
                    if (json.winStatus){                                                  // 是否有获奖标识
                        html +='<div class="people getJiang"  >';
                        html +='<div class="hj"><img src="../img/static/hj.png" alt=""></div>' +
                            '   <div class="jz"><img src="../img/static/jz.png" alt=""></div>';
                    } else {
                        html +='<div class="people"  >';
                    }
                    html +=' <div class="people-img" onclick="lookDetailFunc('+json.id+')">';
                    html +='    <img src="'+json.nominatorPhoto+'" class="img">';
                    if (defalutAwardsDateType==1){
                        html +='    <div class="img-ps">';
                        html +='        票数'+json.votesNum;
                        html +='    </div>';
                    }
                    html +=' </div>';
                    if (json.nominatorType==1) {

                        html +=' <div class="people-info">';
                        html +='    <span class="w">姓名：</span>';
                        html +='    <span>'+json.memberNames+'</span>';
                        html +=' </div>';
                        html +=' <div class="people-info">';
                        html +='    <span class="w">部门：</span>';
                        html +='    <span>'+json.nominatorDeptName+'</span>';
                        html +=' </div>';
                        html +=' <div class="people-info">';
                        html +='    <span class="w">职位：</span>';
                        html +='    <span>'+json.nominatorPosition+'</span>';
                        html +=' </div>' ;
                    }else {
                        html +=' </div>';
                        html +=' <div class="people-info">';
                        html +='    <span class="w">团队：</span>';
                        html +='    <span>'+stringSimplifyFunc(json.memberNames)+'</span>';
                        html +=' </div>';
                        html += ' <div class="people-info">';
                        html += '    <span class="w">部门：</span>';
                        html += '    <span>' + json.applicantDeptName + '</span>';
                        html += ' </div>';
                    }
                    html +=' <div class="people-flower" onclick="shwoMedel('+json.id+')">' ;
                    html +='    <div >' ;
                    html +='        <span class="inb"><img src="../img/static/flower.png" class="fl" alt="送花"> <span class="ml10 fl f12 c9e9e9e">'+json.flowerNum+'</span> </span>' ;
                    html +='    </div>' ;
                    html +='    <div class="l"> ' ;
                    html +='        <span  class="inb"><img src="../img/static/message.png" class="fl" alt="留言"><span class="ml10 fl f12 c9e9e9e">'+json.messageNum+'</span></span>';
                    html +='    </div>' ;
                    html +=' </div>' ;
                    if (defalutAwardsDateType==1 && defalutAwardsSubStatus==1){
                        html +=' <div class="people-btn" id="peopleBtn" onclick="lookDetailFunc('+json.id+')"> 投票 </div>';
                    }else if (defalutAwardsDateType==1){
                        html +=' <div class="people-btn-gray" id="peopleBtn"> 投票 </div>';
                    }
                    html +=' <div class="people-btn"  onclick="lookDetailFunc('+json.id+')">';
                    html +=' 详情';
                    html +=' </div>';
                    html +='</div>';
                    index++;
                });
            }
        } else {
            console.log(resp.msg)
        }
        $("#nameList").empty();
        $("#nameList").append(html);
    });
    onloading = false;
}

/***
 * 折叠或打开
 */
function foldingFunc() {
    var show = $("#aboutBtn").attr("show");
    if (show==1){
        $("#aboutBtn").attr("show",0).text("展开");
        $("#aboutImg").addClass("is-rotate");
        $("#intro-content").toggle(200);
    } else {
        $("#aboutBtn").attr("show",1).text("收起");;
        $("#aboutImg").removeClass("is-rotate");
        $("#intro-content").toggle(200);
    }
}

/***
 * 将字符串大于两个字就换行
 * @param str_
 */
function subStringFunc(str_) {
    var str = str_.replace(/(.{2})/g,'$1<br>');
    var sub = str.substr(str.length-4,4);
    if (sub == '<br>') {
        return str.substring(0,str.length-4);
    }
    return str;
}

/****
 * 新窗口打开
 * @param participatorId
 */
function lookDetailFunc(participatorId) {
    window.open("lookDetail.jsp?id="+participatorId+"&type="+defalutAwardsDateType);
}